//index.js
//获取应用实例
var cfg = require('../../utils/cfg');
var util = require('../../utils/util');
const app = getApp();
const { $Toast } = require('../../dist/base/index');
var common = require('../../utils/common');
var sendRequest = require('../../utils/requestUtils');

Page({
  data: {
    skus: {},
    man : '',
    jian : '',
    xx : ''
  },
  onLoad: function () {
    let that = this;
    that.loadSku();
  },

  loadSku: function () {
    let that = this;
    sendRequest.sendReq(cfg.djurl.listSku, {
      openId: 'xxx',
      skuType: 3
    }, function (result) {
      that.setData({
        skus: result.data
      });
    });
  },

  //保存sku信息
  saveSku: function () {
    let that = this;
    if (that.data.man == '' || that.data.man == undefined) {
      util.alert('请输入消费金额');
      return;
    }

    if (that.data.jian == '' || that.data.jian == undefined) {
      util.alert('请输入优惠金额');
      return;
    }

    let sku = [{'skuType': 3, 'value1': that.data.man, 'value2': that.data.jian}];

    sendRequest.sendReq(cfg.djurl.saveSku, {
      'skuInfo': JSON.stringify(sku),
      'openId': 'xxx',
      'delFirst': false
    }, function (result) {
      let skus = that.data.skus;
      skus.push(sku[0]);

      that.setData({
        skus: skus,
        xx : ''
      });
    });

  },

  delTag: function (event) {
    let that = this;
    console.log('skuId=' + event.detail.name)
    let skuId = event.detail.name;

    sendRequest.sendReq(cfg.djurl.delSku, {
      'skuId': skuId,
      'openId': 'xxx'
    }, function (result) {
      that.loadSku();
    });

  },

  manChange: function (event) {
    this.data.man = event.detail.detail.value;
  },
  jianChange: function (event) {
    this.data.jian = event.detail.detail.value;
  }

})
